﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Pipes.Data;

namespace Pipes.Benchmark
{
  class Program
  {
    static void Main(string[] args)
    {
      StringSplitDelimReader();
      Console.ReadLine();
    }

    static void StringSplitDelimReader()
    {
      var input = "123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,456,1233244323244359438347324912437583421,123,4,56,,78912,456,123,4,56,,78912,45656";
      string curr;
      var delim = new char[] { ',' };
      Console.Write("Old: {0:0.0000}ms", TimeExecution(1000, () => {
        foreach (var val in input.Split(delim, StringSplitOptions.RemoveEmptyEntries))
        {
          curr = val;
        }
      }));
      
      var reader2 = new IO.StringTextSource(input).Pipe(new DelimitedTextReader());
      reader2.SkipEmpty = true;
      reader2.AddDelim(',');
      Console.Write(" New: {0:0.0000}ms", TimeExecution(1000, () =>
      {
        foreach (var val in reader2)
        {
          curr = val;
        }
      }));

      //Console.Write(" CSV: {0:0.0000}ms", TimeExecution(1000, () =>
      //{
      //  using (var csvReader = new LumenWorks.Framework.IO.Csv.CsvReader(new System.IO.StringReader(input), false))
      //  {
      //    csvReader.ReadNextRecord();
      //    for (int i = 0; i < csvReader.FieldCount; i++) curr = csvReader[i];
      //  }
      //}));

      string[] fields;
      Console.Write(" VB: {0:0.0000}ms", TimeExecution(10, () =>
      {
        using (var parser1 = new Microsoft.VisualBasic.FileIO.TextFieldParser(new System.IO.StringReader(input)))
        {
          parser1.SetDelimiters(",");
          while (!parser1.EndOfData)
          {
            fields = parser1.ReadFields();
            for (int i = 0; i < fields.Length; i++) curr = fields[i];
          }
        }
      }));

      Console.WriteLine();
    }

    static double TimeExecution(int loops, Action func)
    {
      var sw = new Stopwatch();
      sw.Start();
      for (var i = 0; i < loops; i++)
      {
        func();
      }
      sw.Stop();
      return (double)sw.ElapsedMilliseconds / loops;
    }
  }
}
